﻿package com.cw.war.ui
{

	/**
	 * 消息弹出框
	 * @author DanielLu
	 */
	public class MessageBox
	{
		// 配置按钮显示方式
		public static const NONE : int = 0;
		public static const ONE : int = 1;
		public static const BOTH : int = 2;

		/**
		 * 显示一个消息框。
		 * @param text					显示的警告文本。
		 * @param method				按钮的显示方式。
		 * @param actionListener		按下确定按钮后执行的方法。
		 * @param cancelListener		按钮取消按钮后执行的方法。
		 */
		public static function open ( text : Object = "" , method : int = 1 , actionListener : Function = null , cancelListener : Function = null ) : void
		{
			Alert.open ( String ( text ) , method , actionListener , cancelListener );
		}
	}
}

import com.cw.war.ui.AbstractPopup;

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;

import net.libra.ui.EasyButton;

class Alert extends AbstractPopup
{
	private static var instance : Alert;

	public static function open ( text : String = "" , method : int = 1 , actionListener : Function = null , cancelListener : Function = null ) : void
	{
		if (instance == null)
		{
			instance = new Alert ();
		}
		instance.text = text;
		instance.method = method;
		instance.actionListener = actionListener;
		instance.cancelListener = cancelListener;
		instance.open ();
	}

	private var mc : MovieClip;
	private var text : String;
	private var method : int;
	private var actionListener : Function;
	private var cancelListener : Function;

	// private var closeBtn : EasyButton;
	private var doBtn : EasyButton;
	private var cancelBtn : EasyButton;
	private var txt : TextField;

	/**
	 * 创建一个<code>Alert</code>组件。
	 * @param text		要显示的文本。
	 */
	public function Alert ()
	{
		mouseEnabled = true;
		mouseChildren = true;
		
		this.mc = new Lib_AlertMC ();
		this.addChild ( mc );

		// this.closeBtn = EasyButton.createEasyButton ( this.mc["closeBtn"] ) ;
		this.doBtn = EasyButton.createEasyButton ( this.mc["doBtn"] ) ;
		this.cancelBtn = EasyButton.createEasyButton ( this.mc["cancelBtn"] ) ;
		this.txt = this.mc["txt"];
		// this.closeBtn.addEventListener ( MouseEvent.CLICK , this.__onCancel );
		this.doBtn.addEventListener ( MouseEvent.CLICK , this.__onAction );
		this.cancelBtn.addEventListener ( MouseEvent.CLICK , this.__onCancel );

		this.txt.multiline = true;
		this.txt.autoSize = TextFieldAutoSize.LEFT;
		this.txt.wordWrap = true;
		this.setModal ( true );
	}
	public function setText(str:String):void
	{
		this.txt.text = str;
	}
	private function __onAction ( e : Event ) : void
	{
		if (this.actionListener != null)
		{
			this.actionListener ();
		}
		this.close ();
	}

	private function __onCancel ( e : Event ) : void
	{
		if (this.cancelListener != null)
		{
			this.cancelListener ();
		}
		this.close ();
	}


	override public function open () : void
	{
		this.txt.text = text;
		this.txt.y = (158 - this.txt.height) / 2;
		if (this.method == 0)
		{
			this.doBtn.visible = false;
			this.cancelBtn.visible = false;
		}
		else if (this.method == 1)
		{
			this.doBtn.visible = true;
			this.cancelBtn.visible = false;
			this.doBtn.x = 123;
		}
		else if (this.method == 2)
		{
			this.doBtn.visible = true;
			this.cancelBtn.visible = true;
			this.doBtn.x = 80;
			this.cancelBtn.x = 158;
		}
		super.open ();
	}
}